package com.example.wallpaperapp.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * @Package: com.example.wallpaperapp.utils
 * @ClassName: SpfUtils
 * @Description: java类作用描述
 * @Author: zrmo
 * @CreateDate: 2020/11/11 21:41
 */

public class SpfUtils {
    protected static String TAG ;
    /**
     * 保存在手机里面的文件名
     */
    private static final String FILE_NAME = "userData";
    private static SharedPreferences prefs;
    public static void init(Context app) {
        prefs = app.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        TAG = app.getClass().getSimpleName();
    }
    public static void setParam(String key, Object object) {
        SharedPreferences.Editor editor = prefs.edit();
        if (object == null) {
            editor.putString(key, "null");
            return;
        } else {
            String type = object.getClass().getSimpleName();
            if ("String".equals(type)) {
                editor.putString(key, (String) object);
            } else if ("Integer".equals(type)) {
                editor.putInt(key, (Integer) object);
            } else if ("Boolean".equals(type)) {
                editor.putBoolean(key, (Boolean) object);
            } else if ("Float".equals(type)) {
                editor.putFloat(key, (Float) object);
            } else if ("Long".equals(type)) {
                editor.putLong(key, (Long) object);
            }
        }
        editor.apply();
    }

    public static Object getParam(String key, Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();

        if ("String".equals(type)) {
            return prefs.getString(key, (String) defaultObject);
        } else if ("Integer".equals(type)) {
            return prefs.getInt(key, (Integer) defaultObject);
        } else if ("Boolean".equals(type)) {
            return prefs.getBoolean(key, (Boolean) defaultObject);
        } else if ("Float".equals(type)) {
            return prefs.getFloat(key, (Float) defaultObject);
        } else if ("Long".equals(type)) {
            return prefs.getLong(key, (Long) defaultObject);
        }
        return null;
    }

    public static void removeData(String key) {
        prefs.edit().remove(key).apply();
    }
    /**
     * 保存用户信息
     */
    private void saveUserInfo(){
        SharedPreferences.Editor editor = prefs.edit();//获取Editor
        //得到Editor后，写入需要保存的数据
        editor.putString("username", "一只猫的涵养");
        editor.putInt("age", 20);
        editor.commit();//提交修改
        Log.i(TAG, "保存用户信息成功");
    }
    /**
     * 读取用户信息
     */
    private void getUserInfo(){
        String username = prefs.getString("username", null);//读取username
        int age = prefs.getInt("age", 0);//读取age
        Log.i(TAG, "读取用户信息");
        Log.i(TAG, "username:" + username + "， age:" + age);
    }
    /**
     * 移除年龄信数据
     */
    private void removeUserInfo(){
        SharedPreferences.Editor editor = prefs.edit();//获取Editor
        editor.remove("age");
        editor.commit();
        Log.i(TAG, "移除年龄数据");
    }

    /**
     * 清空数据
     */
    private void clearUserInfo(){
        SharedPreferences.Editor editor = prefs.edit();//获取Editor
        editor.clear();
        editor.commit();
        Log.i(TAG, "清空数据");
    }
}
